﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.quest.Display {
    import flash.events.*;
    import flash.display.*;
    import Events.*;
    import flash.utils.*;
    import Display.DialogUI.*;
    import org.aswing.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Modules.quest.Managers.*;
    import Display.*;
    import Classes.virals.*;
    import Display.MarketUI.*;
    import flash.text.*;
    import org.aswing.event.*;
    import org.aswing.border.*;
    import Init.*;
    import Display.aswingui.*;
    import Modules.quest.guide.*;
    import Modules.quest.Display.TaskFooters.*;
    import L10n.*;
    import com.adobe.utils.*;
    import Modules.quest.Helpers.*;

    public class QuestPopupView extends GenericDialogView {

        private static const TASK_INCOMPLETE_GAP:int = 91;
        private static const TASK_COMPLETE_GAP:int = 70;
        private static const HINT_TEXT_MIN_WIDTH:int = 320;
        private static const HEADER_TEXT_WIDTH:int = 440;
        private static const GAP_ABOVE_NPC:int = 11;

        protected var m_horizontalRule:DisplayObject;
        protected var m_checkMark:Class;
        protected var m_subQuestField:Class;
        protected var m_taskInfo:Array;
        protected var m_data:Object;
        protected var m_innerInteriorHolder:JPanel;
        protected var m_outerInteriorHolder:JPanel;
        protected var m_closebtnOver:DisplayObject;
        protected var m_npcName:String;
        protected var m_npcIcon:DisplayObject;
        protected var m_locObjects:Object;
        protected var m_sectionTitle:JTextField;
        protected var m_taskFooterFactory:GameQuestTaskFooterFactory;
        protected var m_inventoryList:MarketScrollingList;
        protected var m_okayButton:CustomButton;
        protected var m_questTitle:String;
        protected var m_buttonsListenerRefDict:Dictionary;

        public function QuestPopupView(_arg1:Dictionary, _arg2:Object){
            this.m_data = _arg2;
            this.m_taskFooterFactory = new GameQuestTaskFooterFactory();
            super(_arg1, "", "", 0, null);
            this.setLayout(new SoftBoxLayout(SoftBoxLayout.Y_AXIS, 0, SoftBoxLayout.TOP));
        }
        override protected function init():void{
            this.m_buttonsListenerRefDict = new Dictionary();
            m_bgAsset = m_assetDict["inProgressBG"];
            m_bgInterior = m_assetDict["innerBG"];
            this.m_horizontalRule = m_assetDict["horizontalRule"];
            this.m_checkMark = m_assetDict["checkMark"];
            this.m_subQuestField = m_assetDict["subQuestField"];
            this.m_taskInfo = m_assetDict["taskInfo"];
            this.m_npcName = m_assetDict["npcName"];
            this.m_npcIcon = m_assetDict["npcIcon"];
            this.m_locObjects = m_assetDict["locObjects"];
            this.initTaskFooters();
            this.makeCenterPanel();
            this.makeBackground();
            ASwingHelper.prepare(this);
        }
        protected function initTaskFooters():void{
            this.m_taskFooterFactory.register(TaskFooterType.PLACE_NOW_FROM_INVENTORY, PlaceNowFromInventory);
            this.m_taskFooterFactory.register(TaskFooterType.BEGIN_MINI_GAME, BeginMiniGame);
        }
        override protected function makeBackground():void{
            this.setBackgroundDecorator(new MarginBackground(m_bgAsset, new Insets(0, 0, 16, 0)));
        }
        protected function makeHeader(_arg1:int=20):void{
            this.m_questTitle = ZLoc.t("Quest", (this.m_data.name + "_dialog_title"), this.m_locObjects);
            var _local2:JPanel = new JPanel(new BorderLayout());
            var _local3:Number = TextFieldUtil.getLocaleFontSize(30, 20, null);
            var _local4:TextFormat = new TextFormat();
            _local4.size = (_local3 + 3);
            _local4.align = TextFormatAlign.CENTER;
            _local4.leading = (30 - _local3);
            this.m_sectionTitle = ASwingHelper.makeTextField(this.m_questTitle, EmbeddedArt.TITLE_FONT, _local3, EmbeddedArt.titleColor);
            LocaleHelper.localSetting.ASwingHelper_setEasyBorder(this.m_sectionTitle, 8);
            this.m_sectionTitle.filters = EmbeddedArt.titleFilters;
            TextFieldUtil.formatSmallCaps(this.m_sectionTitle.getTextField(), _local4);
            _local2.append(this.m_sectionTitle, BorderLayout.CENTER);
            var _local5:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
            var _local6:JButton = ASwingHelper.makeMarketCloseButton();
            _local6.addActionListener(onCancelX, 0, true);
            _local5.append(_local6);
            ASwingHelper.setEasyBorder(_local5, 2, 0, 0, 6);
            _local2.append(_local5, BorderLayout.EAST);
            _local2.append(ASwingHelper.horizontalStrut(_local5.getPreferredWidth()), BorderLayout.WEST);
            m_interiorHolder.append(_local2);
        }
        protected function doGuide():void{
            if (((Global.isVisiting()) || (((UI.questManagerView) && (UI.questManagerView.isShown))))){
                return;
            };
            if (((!(((this.m_data.guide as QuestGuide) == null))) && (!((this.m_data.guideName == null))))){
                (this.m_data.guide as QuestGuide).notify(this.m_data.guideName);
            };
        }
        override protected function onCancel(_arg1):void{
            this.cleanUp();
            super.onCancel(_arg1);
            this.doGuide();
        }
        private function makeSubHeader():void{
            var _local1:String = ZLoc.t("Quest", (this.m_data.name + "_dialog_header"), this.m_locObjects);
            if ((((_local1.indexOf("Cannot find string") == 0)) || ((_local1.length <= 0)))){
                return;
            };
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
            _local2.setMinimumWidth((HEADER_TEXT_WIDTH + 40));
            var _local3:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            _local3.setPreferredWidth(HEADER_TEXT_WIDTH);
            var _local4:AssetPane = ASwingHelper.makeMultilineText(_local1, HEADER_TEXT_WIDTH, EmbeddedArt.DEFAULT_FONT_NAME_BOLD, TextFormatAlign.LEFT, 18, 489375);
            _local3.append(_local4);
            _local2.append(_local3);
            m_interiorHolder.append(_local2);
        }
        override protected function makeCenterPanel():void{
            m_interiorHolder = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
            this.makeHeader();
            this.makeSubHeader();
            this.m_outerInteriorHolder = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
            this.m_innerInteriorHolder = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
            this.makeTaskRows();
            this.makeHintandButton();
            this.makeTaskFooters();
            ASwingHelper.prepare(m_interiorHolder);
            this.append(m_interiorHolder);
            ASwingHelper.prepare(this);
        }
        protected function makeHintandButton():void{
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.LEFT);
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
            var _local3:AssetPane = new AssetPane(this.m_npcIcon);
            _local2.append(ASwingHelper.verticalStrut((GAP_ABOVE_NPC - 1)));
            _local2.append(_local3);
            _local1.append(ASwingHelper.horizontalStrut(5));
            _local1.append(_local2);
            _local1.append(ASwingHelper.horizontalStrut(-5));
            _local1.setPreferredHeight((this.m_npcIcon.height + GAP_ABOVE_NPC));
            var _local4:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
            _local4.setPreferredHeight((this.m_npcIcon.height + GAP_ABOVE_NPC));
            _local4.append(ASwingHelper.verticalStrut((GAP_ABOVE_NPC - 3)));
            var _local5:JTextField = ASwingHelper.makeTextField(ZLoc.t("Quest", (this.m_npcName + "_hint_caption"), this.m_locObjects), EmbeddedArt.DEFAULT_FONT_NAME_BOLD, 14, 0xFFFFFF);
            _local4.append(_local5);
            var _local6:JPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
            var _local7:int = HINT_TEXT_MIN_WIDTH;
            if (this.m_sectionTitle.getPreferredWidth() > HEADER_TEXT_WIDTH){
                _local7 = (_local7 + (this.m_sectionTitle.getPreferredWidth() - HEADER_TEXT_WIDTH));
            };
            var _local8:AssetPane = ASwingHelper.makeMultilineText(ZLoc.t("Quest", (this.m_data.name + "_dialog_hint"), this.m_locObjects), (_local7 + 20), EmbeddedArt.DEFAULT_FONT_NAME_BOLD, TextFormatAlign.LEFT, 18, 489375);
            _local6.append(ASwingHelper.horizontalStrut(22));
            _local6.append(_local8);
            _local6.append(ASwingHelper.horizontalStrut(15));
            _local4.append(_local6);
            _local1.append(_local4);
            var _local9:JPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            this.m_okayButton = new CustomButton(ZLoc.t("Dialogs", "Okay"), null, "GreenButtonUI");
            this.m_okayButton.addActionListener(this.closePanel, 0, true);
            _local9.append(this.m_okayButton);
            this.m_outerInteriorHolder.setBorder(new EmptyBorder(null, new Insets(0, 6, 0, 3)));
            this.m_outerInteriorHolder.append(this.m_innerInteriorHolder);
            this.m_outerInteriorHolder.append(_local1);
            m_interiorHolder.append(this.m_outerInteriorHolder);
            m_interiorHolder.append(ASwingHelper.verticalStrut(-24));
            m_interiorHolder.append(_local9);
            m_interiorHolder.append(ASwingHelper.verticalStrut(6));
        }
        protected function closePanel(_arg1:AWEvent):void{
            if (this.m_acceptTextName.length > 0){
                countDialogViewAction("OKAY");
            };
            this.cleanUp();
            dispatchEvent(new Event(Event.CLOSE, false, false));
            this.doGuide();
        }
        public function get okayButton():CustomButton{
            return (this.m_okayButton);
        }
        protected function makePurchaseCallback(_arg1:int, _arg2:int):Function{
            var taskIndex:* = _arg1;
            var cashcost:* = _arg2;
            return (function (_arg1:AWEvent):void{
                var _local3:*;
                var _local2:* = QuestSettingsInit.getItemByName(m_data.name);
                countDialogViewAction("PURCHASE", false);
                if (Global.player.cash < cashcost){
                    cleanUp();
                    dispatchEvent(new Event(Event.CLOSE, false, false));
                    UI.displayPopup(new GetCoinsDialog(ZLoc.t("Dialogs", "ImpulseMarketCash"), "", GenericDialogView.TYPE_YESNO, null, true));
                } else {
                    Global.player.cash = (Global.player.cash - cashcost);
                    if (_local2.tasks[taskIndex].action == "checkItemsAtInit"){
                        _local3 = (_local2.tasks[taskIndex].total - Global.player.inventory.getItemCountByName(_local2.tasks[taskIndex].type));
                        Global.player.inventory.addItems(_local2.tasks[taskIndex].type, _local3, true);
                        if (_local2.tasks[taskIndex].type == Item.HOLIDAY2010_PRESENT_ITEM){
                            HolidayTree.instance.m_mostPresents = _local2.tasks[taskIndex].total;
                            HolidayTree.instance.reloadImage();
                        };
                    };
                    GameTransactionManager.addTransaction(new TBuyQuestProgress(m_data.name, taskIndex));
                    Global.questManager.setQuestTaskProgress(m_data.name, taskIndex, _local2.tasks[taskIndex].total);
                    _local2.purchased[taskIndex] = true;
                };
                if ((((_local2 is GameQuest)) && ((_local2 as GameQuest).isQuestComplete()))){
                    cleanUp();
                    dispatchEvent(new Event(Event.CLOSE, false, false));
                } else {
                    invalidateRows();
                };
            });
        }
        public function invalidateRows():void{
            this.cleanUp();
            this.removeAll();
            this.setBackgroundDecorator(null);
            this.makeCenterPanel();
            this.makeBackground();
            dispatchEvent(new UIEvent(UIEvent.REFRESH_DIALOG, "", true));
            ASwingHelper.prepare(this);
        }
        protected function makeFeedCallback(_arg1:String, _arg2:CustomButton):Function{
            var feedName:* = _arg1;
            var button:* = _arg2;
            return (function (_arg1:AWEvent):void{
                if (feedName == ViralType.NEIGHBOR_GATE_QUEST_REQUEST){
                    FrameManager.showTray("invite.php?view=areapp&ref=non_cityville_MFS&neighborgate=1");
                    return;
                };
                Global.world.viralMgr.sendQuestFeed(Global.player, feedName);
                button.setEnabled(false);
            });
        }
        protected function makeRedeemXPromoCallback(_arg1:String, _arg2:CustomButton):Function{
            var xPromoRewardName:* = _arg1;
            var button:* = _arg2;
            return (function (_arg1:AWEvent):void{
                var _local2:* = m_data["name"];
                var _local3:* = xPromoRewardName;
                var _local4:* = ((("xpromo.php?action=redeem&rewardName=" + _local3) + "&questName=") + _local2);
                FrameManager.navigateTo(_local4);
                button.setEnabled(false);
            });
        }
        private function makeTaskFooters():void{
            var _local1:ITaskFooter;
            var _local2:Component;
            var _local3:XML;
            if (this.m_data.taskFooters){
                for each (_local3 in this.m_data.taskFooters.taskFooter) {
                    _local1 = this.m_taskFooterFactory.createTaskFooter(_local3.@action, _local3.@type, this);
                    _local2 = _local1.getComponent();
                    if (_local2 != null){
                        this.m_innerInteriorHolder.append(_local1.getComponent());
                    };
                };
            };
        }
        private function makeTaskRows():void{
            var _local5:JPanel;
            var _local6:DisplayObject;
            var _local7:AssetPane;
            var _local8:DisplayObject;
            var _local9:JPanel;
            var _local10:JPanel;
            var _local11:JPanel;
            var _local12:int;
            var _local13:AssetPane;
            var _local14:JPanel;
            var _local15:DisplayObject;
            var _local16:JTextField;
            var _local17:String;
            var _local18:String;
            var _local19:JPanel;
            var _local20:int;
            var _local21:String;
            var _local22:JPanel;
            var _local23:AssetIcon;
            var _local24:String;
            var _local25:CustomButton;
            var _local26:JPanel;
            var _local27:String;
            var _local28:CustomButton;
            var _local29:JPanel;
            var _local30:CustomButton;
            var _local31:JTextField;
            var _local1:JPanel = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
            var _local2:JPanel = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER, -3);
            var _local3:Quest = QuestSettingsInit.getItemByName(this.m_data.name);
            var _local4:int;
            while (_local4 < this.m_taskInfo.length) {
                if ((((this.m_taskInfo[_local4].name == "seenQuest")) || ((this.m_taskInfo[_local4].name == "popNews")))){
                } else {
                    _local5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
                    _local6 = this.m_taskInfo[_local4].image.content;
                    this.m_taskInfo[_local4].image.content.width = (_local6.height = 60);
                    if ((_local6 is Bitmap)){
                        (_local6 as Bitmap).smoothing = true;
                    };
                    _local7 = new AssetPane(_local6);
                    _local8 = (new this.m_subQuestField() as DisplayObject);
                    _local9 = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.CENTER);
                    _local10 = ASwingHelper.makeFlowJPanel(FlowLayout.LEFT);
                    _local9.append(_local10);
                    _local9.setBackgroundDecorator(new MarginBackground(_local8, new Insets(0, 0, 0, 1)));
                    _local11 = new JPanel(new FlowLayout(FlowLayout.LEFT));
                    _local10.append(_local11);
                    _local12 = Math.round((_local8.width * 0.6));
                    _local13 = ASwingHelper.makeMultilineText(this.m_taskInfo[_local4].text, _local12, EmbeddedArt.DEFAULT_FONT_NAME_BOLD, TextFormatAlign.LEFT, 18, EmbeddedArt.brownTextColor);
                    _local14 = ASwingHelper.makeSoftBoxJPanelVertical(SoftBoxLayout.TOP);
                    _local14.append(_local13);
                    _local11.append(_local14);
                    if ((((Global.questManager.getQuestProgressByName(this.m_data.name).progress[_local4] >= this.m_data.tasks[_local4].@total)) || (_local3.purchased[_local4]))){
                        _local15 = (new this.m_checkMark() as DisplayObject);
                        _local11.append(new AssetPane(_local15));
                    } else {
                        _local17 = this.m_data.progress[_local4];
                        if (this.m_data.tasks[_local4].@percent == "true"){
                            if (_local17 == "100"){
                                _local17 = "99";
                            };
                            _local18 = ZLoc.t("Dialogs", "QuestPercentXofY", {
                                completed:_local17,
                                total:this.m_data.tasks[_local4].@total
                            });
                        } else {
                            _local18 = ZLoc.t("Dialogs", "QuestStepsXofY", {
                                completed:_local17,
                                total:this.m_data.tasks[_local4].@total
                            });
                        };
                        _local18 = (_local18 + "  ");
                        _local16 = ASwingHelper.makeTextField(_local18, EmbeddedArt.TITLE_FONT, 18, EmbeddedArt.brownTextColor);
                        _local11.append(_local16);
                        _local19 = ASwingHelper.makeSoftBoxJPanel();
                        _local20 = int(this.m_data.tasks[_local4].@cashcost);
                        if (_local20 > 0){
                            _local22 = new JPanel(new FlowLayout(FlowLayout.CENTER));
                            _local19.append(_local22);
                            _local23 = new AssetIcon((new EmbeddedArt.icon_cash() as DisplayObject));
                            _local24 = ZLoc.t("Dialogs", "QuestCashComplete", {price:_local20});
                            _local25 = new CustomButton(_local24, _local23, "CashButtonUI");
                            _local25.setPreferredHeight(30);
                            _local25.setMargin(new Insets(2, 2, 0, 2));
                            this.m_buttonsListenerRefDict[_local25] = this.makePurchaseCallback(_local4, _local20);
                            _local25.addActionListener(this.m_buttonsListenerRefDict[_local25]);
                            _local22.append(_local25);
                        };
                        if (this.m_data.tasks[_local4].@action == "redeemXPromo"){
                            _local26 = new JPanel(new FlowLayout(FlowLayout.CENTER));
                            _local26.setPreferredWidth(_local11.getPreferredWidth());
                            _local9.append(_local26);
                            _local27 = this.m_data.tasks[_local4].@reward_name;
                            _local28 = new CustomButton(ZLoc.t("Dialogs", "ClaimReward"), null, "GreenButtonUI");
                            this.m_buttonsListenerRefDict[_local28] = this.makeRedeemXPromoCallback(_local27, _local28);
                            _local28.addActionListener(this.m_buttonsListenerRefDict[_local28]);
                            _local26.append(_local28);
                        };
                        _local21 = String(this.m_data.tasks[_local4].@feed);
                        if (((!((_local21 == null))) && (!((_local21 == ""))))){
                            _local29 = new JPanel(new FlowLayout(FlowLayout.CENTER));
                            _local19.append(_local29);
                            if (Global.world.viralMgr.canPost(_local21)){
                                _local30 = new CustomButton(ZLoc.t("Dialogs", "QuestSendFeed"), null, "GreenButtonUI");
                                _local30.setPreferredHeight(30);
                                this.m_buttonsListenerRefDict[_local30] = this.makeFeedCallback(_local21, _local30);
                                _local30.addActionListener(this.m_buttonsListenerRefDict[_local30]);
                                _local29.append(_local30);
                            } else {
                                _local31 = ASwingHelper.makeTextField((ZLoc.t("Dialogs", "QuestNoFeed") + "  "), EmbeddedArt.DEFAULT_FONT_NAME, 14, 0x404040);
                                _local29.append(_local31);
                            };
                        };
                        _local9.append(_local19);
                    };
                    ASwingHelper.prepare(_local10);
                    ASwingHelper.prepare(_local9);
                    _local5.append(_local7);
                    _local5.append(ASwingHelper.horizontalStrut(4));
                    _local5.append(_local9);
                    _local2.append(_local5);
                };
                _local4++;
            };
            _local1.append(_local2);
            this.m_innerInteriorHolder.append(_local1);
        }
        protected function cleanUp():void{
            var _local2:CustomButton;
            var _local1:Array = DictionaryUtil.getKeys(this.m_buttonsListenerRefDict);
            for each (_local2 in _local1) {
                _local2.removeActionListener(this.m_buttonsListenerRefDict[_local2]);
            };
        }

    }
}//package Modules.quest.Display 
